import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:yi_shop/route/index.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/svg.dart';
import 'package:yi_shop/utils/tools.dart';

class MineSignInController extends GetxController {
  Rx<DateTime> currentTime = Rx(DateTime.now());

  DateTime nowTime = DateTime.now();

  RxList<DateTime> days = RxList([]);

  @override
  void onInit() {
    startDays(nowTime.year, nowTime.month + 1);
    super.onInit();
  }

  void startDays(int year, int month) {
    DateTime d = DateTime(year, month, 0);
    List<DateTime> days = [];
    for (var i = 1; i <= d.day; i++) {
      var t = DateTime(d.year, d.month, i);
      if (i == 1) {
        for (var j = 1; j < t.weekday; j++) {
          var b = t.subtract(Duration(days: j));
          days.add(b);
        }
      }
      days.add(t);
      if (i == d.day) {
        var len = 7 - t.weekday;
        for (var j = 1; j <= len; j++) {
          var a = t.add(Duration(days: j));
          days.add(a);
        }
      }
    }
    this.days.value = days;
  }

  void changeMonth(int year, int month) {
    currentTime.value = DateTime(year, month);
    startDays(currentTime.value.year, currentTime.value.month + 1);
  }
}

class MineSignInPage extends GetView<MineSignInController> {
  const MineSignInPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: const Text('签到'),
        bottom: PreferredSize(
          preferredSize: const Size.fromHeight(172),
          child: SizedBox(
            height: 172,
            child: Stack(
              children: [
                Container(
                  padding: const EdgeInsets.only(top: 10),
                  alignment: Alignment.topCenter,
                  child: SizedBox(
                    width: 120,
                    height: 120,
                    child: CustomPaint(
                      painter: _MyCustomPaint(),
                      child: Column(
                        children: [
                          Expanded(
                              child: Container(
                            alignment: Alignment.bottomCenter,
                            padding: const EdgeInsets.only(bottom: 10),
                            child: Text(
                              '签到',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color3333, fontSize: StylesUtils.fontSize18),
                            ),
                          )),
                          Expanded(
                              child: Container(
                            padding: const EdgeInsets.only(top: 10),
                            alignment: Alignment.topCenter,
                            child: Text(
                              '累计13天',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color6666, fontSize: StylesUtils.fontSize13),
                            ),
                          ))
                        ],
                      ),
                    ),
                  ),
                ),
                Positioned(
                    top: 0,
                    right: 0,
                    child: TextButton(
                      onPressed: () {
                        Get.toNamed(RoutePath.mineSignRule);
                      },
                      style: const ButtonStyle(
                          alignment: Alignment.center,
                          backgroundColor: MaterialStatePropertyAll(Colors.white),
                          padding: MaterialStatePropertyAll(EdgeInsets.zero),
                          fixedSize: MaterialStatePropertyAll(Size(78, 30)),
                          minimumSize: MaterialStatePropertyAll(Size(78, 30)),
                          shape: MaterialStatePropertyAll(RoundedRectangleBorder(
                              borderRadius: BorderRadius.only(
                                  topLeft: Radius.circular(15), bottomLeft: Radius.circular(15))))),
                      child: Text(
                        '签到规则',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color3333, fontSize: StylesUtils.fontSize13),
                      ),
                    )),
                Positioned(
                    left: 0,
                    right: 0,
                    bottom: 10,
                    child: Center(
                      child: Text(
                        '累计签到130天即可获得20成长值',
                        style: StylesUtils.customTextStyle(),
                      ),
                    ))
              ],
            ),
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              alignment: Alignment.center,
              padding: const EdgeInsets.symmetric(vertical: 20),
              child: Container(
                constraints: const BoxConstraints.expand(width: 300, height: 32),
                padding: const EdgeInsets.symmetric(horizontal: 12),
                decoration: BoxDecoration(
                    color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048),
                    borderRadius: BorderRadius.circular(16)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    InkWell(
                        onTap: () {
                          controller.changeMonth(controller.currentTime.value.year,
                              controller.currentTime.value.month - 1);
                        },
                        child: SvgUtils.assetName('u6609', width: 9, height: 15)),
                    Expanded(
                        child: Center(
                      child: Obx(() => Text(
                            DateFormat('yyyy年MM月').format(controller.currentTime.value),
                            style: StylesUtils.customTextStyle(fontSize: StylesUtils.fontSize13),
                          )),
                    )),
                    InkWell(
                        onTap: () {
                          controller.changeMonth(controller.currentTime.value.year,
                              controller.currentTime.value.month + 1);
                        },
                        child: SvgUtils.assetName('u6610', width: 9, height: 15)),
                  ],
                ),
              ),
            ),
            SizedBox(
              height: 30,
              child: StaggeredGrid.count(
                crossAxisCount: 7,
                crossAxisSpacing: 0,
                mainAxisSpacing: 0,
                children: [
                  Center(
                    child: Text(
                      '一',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                  ),
                  Center(
                    child: Text(
                      '二',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                  ),
                  Center(
                    child: Text(
                      '三',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                  ),
                  Center(
                    child: Text(
                      '四',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                  ),
                  Center(
                    child: Text(
                      '五',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                  ),
                  Center(
                    child: Text(
                      '六',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                  ),
                  Center(
                    child: Text(
                      '日',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                  ),
                ],
              ),
            ),
            Obx(() => StaggeredGrid.count(
                  crossAxisCount: 7,
                  crossAxisSpacing: 0,
                  mainAxisSpacing: 5,
                  children: controller.days
                      .map((day) => Container(
                            height: 40,
                            alignment: Alignment.center,
                            child: Text(
                              '${day.day > 9 ? day.day : '0${day.day}'}',
                              style: StylesUtils.customTextStyle(color: getYearMonthColor(day)),
                            ),
                          ))
                      .toList(),
                )),
            Container(
              padding: const EdgeInsets.only(left: 15, top: 35),
              alignment: Alignment.centerLeft,
              child: Text(
                '*补签需要消耗5个贡献值',
                style: StylesUtils.customTextStyle(color: ColorsUtils.color6666),
              ),
            )
          ],
        ),
      ),
    );
  }

  String getYearMonthColor(DateTime day) {
    if (day.year == controller.nowTime.year && day.month == controller.nowTime.month) {
      return ColorsUtils.color3333;
    }
    return ColorsUtils.color9999;
  }
}

class _MyCustomPaint extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint();
    paint.style = PaintingStyle.fill;
    paint.color = ColorsUtils.parseColorFromHexString(ColorsUtils.colorE4e4);
    canvas.drawCircle(Offset(size.width / 2, size.height / 2), size.width / 2, paint);
    paint.color = Colors.white;
    canvas.drawCircle(Offset(size.width / 2, size.width / 2), 50, paint);
    paint.strokeWidth = 1;
    paint.style = PaintingStyle.stroke;
    paint.strokeCap = StrokeCap.round;
    paint.color = ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc);
    canvas.drawLine(Offset((size.width - 80) / 2, size.height / 2),
        Offset((size.width - 80) / 2 + 80, size.height / 2), paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}
